/**
 * 
 */
package photosalvage.entity;

import javax.persistence.TypedQuery;

import photosalvage.util.JPAUtils;

import com.google.inject.persist.Transactional;

/**
 * @author shumpei
 *
 */
public class PlaceRepository extends BaseRepository<Place, Long> {

	@Transactional
	public Place findByNumber(String number) {
		TypedQuery<Place> q = getEntityManager().createQuery("select p from Place p where p.number = :placeNum", Place.class);
		q.setParameter("placeNum", number);
		return JPAUtils.getSingleOrNull(q);
	}
	
	@Transactional
	public Place findOrCreateByNumber(String number) {
		Place p = findByNumber(number);
		if (p == null) {
			p = new Place();
			p.setNumber(number);
			persist(p);
		}
		return p;
	}
}
